package com.bwei.like.mynewstop.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======
import android.view.WindowManager;
>>>>>>> NewsTop
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bwei.like.mynewstop.Bean.CityInfo;
import com.bwei.like.mynewstop.R;
import com.bwei.like.mynewstop.adapater.CityListItemAdapater;
import com.bwei.like.mynewstop.manager.DbManager;
import com.bwei.like.mynewstop.utils.CommonUtils;
import com.bwei.like.mynewstop.view.LetterView;

import java.util.ArrayList;
import java.util.HashMap;

<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
public class CityListActivity extends AppCompatActivity implements View.OnClickListener {
=======
public class CityListActivity extends BaseActivity implements View.OnClickListener {
>>>>>>> NewsTop

    private ListView listView;
    private LetterView letterView;
    private HashMap<String, Integer> hashMap = new HashMap<>();
    private Toast toast;
    private TextView tv_toast;
    private ArrayList<CityInfo> cityList;
    private ImageView back_image;

    @Override
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_city_list);
        initView();


=======
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_city_list);
        initView();

>>>>>>> NewsTop
        //调用数据库管理的方法将数据拷贝到sd下
        DbManager manager = new DbManager(this);
        manager.cope();
        cityList = manager.getCityList();
        //将A B C D存入map集合中
        for (int i = 0; i < cityList.size(); i++) {
            if (cityList.get(i).isFlag()) {
                hashMap.put(cityList.get(i).getNameSort(), i);
            }
        }

        listView.setAdapter(new CityListItemAdapater(cityList, this));
    }

    private void initView() {
        back_image = (ImageView)findViewById(R.id.cityListBackImage);
        back_image.setOnClickListener(this);

        listView = (ListView) findViewById(R.id.listView_localCity);
        letterView = (LetterView) findViewById(R.id.letterView);
        //自定义吐司
        toast = new Toast(this);
        View view = CommonUtils.inflter(R.layout.toast_item);
        tv_toast = (TextView) view.findViewById(R.id.tv_toast);
        toast.setView(view);
        //设置监听回调
        letterView.setOnChangeListener(new LetterView.OnLetterChangeListener() {
            @Override
            public void setOnLetterChange(String s) {
                // Toast.makeText(CityListActivity.this, "点击了"+s, Toast.LENGTH_SHORT).show();
                //将该条目对应的下标取出来
                try {
                    Integer index = hashMap.get(s);
                    listView.setSelection(index);
                    tv_toast.setText(s);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toast.show();
                } catch (Exception e) {

                }
            }
        });

        //设置listview监听事件，将页面回跳回去
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String cityName = cityList.get(position).getCityName();
                Intent intent = new Intent();
                intent.putExtra("city", cityName);
                setResult(101, intent);
                finish();
            }
        });
    }


    /**
     * 返回按钮监听事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        finish();
        overridePendingTransition(R.anim.phone_in,R.anim.cell_regeist_out);
    }
}
